package com.beaven.imclient.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.beaven.imclient.R;
import com.beaven.imclient.activity.ChatActivity;
import com.beaven.imclient.adapter.ChatUserAdapter;
import com.beaven.imclient.base.BaseFragment;
import com.beaven.imclient.presenter.chat.ChatGroupContract;
import com.beaven.imclient.presenter.chat.ChatGroupPresenter;
import com.beaven.model.data.helper.GroupHelper;
import com.beaven.model.model.db.Group;
import com.beaven.model.model.db.Message;
import com.beaven.model.utils.DiffUiDataCallback;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ChatGroupFragment extends BaseFragment implements ChatGroupContract.View {

  public static ChatGroupFragment newInstance(String receiverId) {
    ChatGroupFragment chatGroupFragment = new ChatGroupFragment();
    Bundle bundle = new Bundle();
    bundle.putString(ChatActivity.KEY_RECEIVER_ID, receiverId);
    chatGroupFragment.setArguments(bundle);
    return chatGroupFragment;
  }

  @Override protected int getLayoutId() {
    return R.layout.fragment_chate_group;
  }

  private String receiverId;
  private Group group;
  private ChatUserAdapter chatUserAdapter;
  private RecyclerView recyclerView;
  private ChatGroupContract.Presenter presenter;
  private EditText edit_input_msg;

  @Override protected void initArgs(Bundle arguments) {
    super.initArgs(arguments);
    receiverId = arguments.getString(ChatActivity.KEY_RECEIVER_ID);
    group = GroupHelper.findFromLocal(receiverId);
  }

  @Override protected void initView(View rootView) {
    super.initView(rootView);

    recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_chat_group);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    chatUserAdapter = new ChatUserAdapter(getContext());
    recyclerView.setAdapter(chatUserAdapter);
    scrollRecycler();

    edit_input_msg = (EditText) rootView.findViewById(R.id.edit_input_msg);
    TextView bt_send_msg = (TextView) rootView.findViewById(R.id.bt_send_msg);
    bt_send_msg.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        String input = edit_input_msg.getText().toString().trim();
        if (TextUtils.isEmpty(input)) {
          return;
        }
        presenter.sendMsg(input);
        edit_input_msg.getText().clear();
      }
    });
  }

  @Override protected void initData() {
    super.initData();
    setPresenter(new ChatGroupPresenter(this, receiverId));
  }

  @Override public void onResume() {
    super.onResume();
    presenter.subscribe();
  }

  @Override public void onPause() {
    super.onPause();
    presenter.unSubscribe();
  }

  private void scrollRecycler() {
    List<Message> msg = chatUserAdapter.getMessageList();
    if (msg == null || msg.size() < 1) {
      return;
    }
    recyclerView.scrollToPosition(msg.size() - 1);
  }

  @Override public void setPresenter(ChatGroupContract.Presenter presenter) {
    this.presenter = presenter;
  }

  @Override public void showList(List<Message> messageList) {
    List<Message> old = chatUserAdapter.getMessageList();
    DiffUiDataCallback<Message> callback = new DiffUiDataCallback<>(old, messageList);
    DiffUtil.DiffResult result = DiffUtil.calculateDiff(callback);
    chatUserAdapter.getMessageList().clear();
    chatUserAdapter.getMessageList().addAll(messageList);
    result.dispatchUpdatesTo(chatUserAdapter);
    scrollRecycler();
  }
}
